package com.yiren.dbaa.module.practice_set.site.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.module.practice_set.site.view.adapter.SelectUserAdapter;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/10/14 14:43
 * @description: 选择用户dialog
 */
public class SelectUserDialog {

    public static void show(Context context, String title, int selectIndex, List<String> list, OnSureClickListener listener) {
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCancelable(false);
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        params.width = (metric.widthPixels * 6) / 10;
        params.height = (metric.heightPixels * 8) / 8;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_user);
        ListView lvUser = (ListView) window.findViewById(R.id.lv_user);
        Button btnSure = (Button) window.findViewById(R.id.bt_dialog_ok);
        Button btnCancel = window.findViewById(R.id.bt_dialog_cancel);
        TextView tv_title = (TextView) window.findViewById(R.id.txt_dialog_title);
        tv_title.setText(title);
        SelectUserAdapter adapter = new SelectUserAdapter(context, list, selectIndex);
        lvUser.setAdapter(adapter);
        btnSure.setOnClickListener(v -> {
            listener.onSureClick(adapter.getCheckPosition());
            dialog.dismiss();
        });
        btnCancel.setOnClickListener(v -> dialog.dismiss());
    }

    public interface OnSureClickListener {

        void onSureClick(int selectIndex);
    }

}